package com.zhongfl.crm.conf;


import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.TimeUnit;

/**
 * Web 配置
 * @author tiantian
 * @date 2018/09/19
 */
@Configuration
public class WebConfig implements WebMvcConfigurer, ErrorPageRegistrar {

    /**
     * 静态资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .setCacheControl(CacheControl.maxAge(10, TimeUnit.MINUTES));
    }

    /**
     * 错误页面处理
     *
     * @return 错误信息
     */
    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        ErrorPage[] errorPages=new ErrorPage[3];
        errorPages[0]=new ErrorPage(HttpStatus.NOT_FOUND,"/error404");
        errorPages[1]=new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/error500");
        errorPages[2]=new ErrorPage(HttpStatus.FORBIDDEN, "/error_403");

        registry.addErrorPages(errorPages);
    }

}
